package com.nsgf.library.ui.page.warehouse;

import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED;
import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_DRAGGING;
import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_HIDDEN;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.bean.WareHouseBean;
import com.nsgf.library.databinding.ActivityNsgfWareHouseDetailsBinding;
import com.nsgf.library.ui.adapter.WareHouseMaterialsListAdapter;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 仓库展开详情
 */
public class WareHouseInfoActivity extends BaseActivity {

    StateHolder mState;

    private WareHouseMaterialsListAdapter mAdapter;

    private WareHouseBean bean;

    @Override
    protected void initViewModel() {
        mState = getActivityScopeViewModel(StateHolder.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new WareHouseMaterialsListAdapter(this);

        return new DataBindingConfig(R.layout.activity_nsgf_ware_house_details, BR.vm, mState)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }


    BottomSheetBehavior mBehavior;

    private ActivityNsgfWareHouseDetailsBinding mBinding;

    private List<LibraryInfo> mDatas = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding = (ActivityNsgfWareHouseDetailsBinding) getBinding();
        bean = (WareHouseBean) getIntent().getSerializableExtra("bean");
        mState.wareHouseBeanState.set(bean);
        mState.titleBar.set(mState.wareHouseBeanState.get().getStoreName());
        if (!StringUtil.isEmpty(bean.getWovenBagUnitDesc()))
            mDatas.add(new LibraryInfo("编织袋(" + bean.getWovenBagUnitDesc() + ")", bean.getWovenBag(), "", R.drawable.ic_nsgf_ware_materials_dai));

        if (!StringUtil.isEmpty(bean.getGeotextileUnitDesc()))
            mDatas.add(new LibraryInfo("土工布(" + bean.getGeotextileUnitDesc() + ")", bean.getGeotextile(), "", R.drawable.ic_nsgf_ware_materials_bu));

        if (!StringUtil.isEmpty(bean.getStoneUnitDesc()))
            mDatas.add(new LibraryInfo("块石(" + bean.getStoneUnitDesc() + ")", bean.getStone(), "", R.drawable.ic_nsgf_ware_materials_sk));

        if (!StringUtil.isEmpty(bean.getBurlapBagUnitDesc()))
            mDatas.add(new LibraryInfo("麻袋(" + bean.getBurlapBagUnitDesc() + ")", bean.getBurlapBag(), "", R.drawable.ic_nsgf_ware_materials_md));

        if (!StringUtil.isEmpty(bean.getWoodUnitDesc()))
            mDatas.add(new LibraryInfo("木材(" + bean.getWoodUnitDesc() + ")", bean.getWood(), "", R.drawable.ic_nsgf_ware_materials_mc));

        if (!StringUtil.isEmpty(bean.getIronWireUnitDesc()))
            mDatas.add(new LibraryInfo("铁丝(" + bean.getIronWireUnitDesc() + ")", bean.getIronWire(), "", R.drawable.ic_nsgf_ware_materials_ts));

        if (!StringUtil.isEmpty(bean.getColoredStripedFabricUnitDesc()))
            mDatas.add(new LibraryInfo("彩布条(" + bean.getColoredStripedFabricUnitDesc() + ")", bean.getColoredStripedFabric(), "", R.drawable.ic_nsgf_ware_materials_cbd));

        mState.testList.set(mDatas);

        mBehavior = BottomSheetBehavior.from(mBinding.bottomSheet);
        mBehavior.setState(STATE_COLLAPSED);

        mBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

                if (newState == STATE_HIDDEN) {
                    mBehavior.setState(STATE_COLLAPSED);
                    mBinding.ivNsgfHomeSheet.setImageResource(R.drawable.ic_nsgf_home_sheet_up);
                } else if (newState == STATE_COLLAPSED) {
                    mBinding.ivNsgfHomeSheet.setImageResource(R.drawable.ic_nsgf_home_sheet_up);
                } else {
                    if (newState != STATE_DRAGGING)
                        mBinding.ivNsgfHomeSheet.setImageResource(R.drawable.ic_nsgf_home_sheet_down);
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });


        String url = "file:///android_asset/roadLine/index.html";
        WebSettings mWebSettings = mBinding.webView.getWebView().getSettings();
        mWebSettings.setJavaScriptEnabled(true);//设置支持javaScript
        mBinding.webView.loadUrl(url);
        // 当页面加载完成后调用JavaScript方法
        mBinding.webView.getWebView().setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                String[] points = mState.wareHouseBeanState.get().getCoordinates().split(",");
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("lon", points[0]);
                jsonObject.addProperty("lat", points[1]);
                callJavaScriptZoomToPointFunction(jsonObject);
            }
        });


    }


    private void callJavaScriptZoomToPointFunction(JsonObject object) {
        JsonArray array = new JsonArray();
        object.addProperty("status", "77");
        array.add(object);
        mBinding.webView.getWebView().evaluateJavascript("zoomToPointProject(" + object + "," + "'" + array + "'" + ");", null);
    }

    public class ClickProxy {

        public void navigation() {
        }
    }
}
